package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DML语句(INSERT,UPDATE,DELETE)
 */
public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        try(
                Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                        "root",
                        "root");
        ) {
            Statement state = connection.createStatement();
            String sql = "INSERT INTO student1(name,age,class_id) " +
                    "     VALUES('张三',12,1)";
            /*
                int executeUpdate(String sql)
                专门用来执行DML语句的方法，返回值为执行该SQL后影响了表中多少条记录
             */
            int count = state.executeUpdate(sql);//INSERT插入1条数据
            if(count>0){//执行后至少影响了表中1条数据
                System.out.println("影响了表中"+count+"条数据");
            }


            System.out.println("执行完毕");
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
